package cn.mafangui.hotel.controller.worker;

import cn.mafangui.hotel.entity.Notice;
import cn.mafangui.hotel.entity.Order;
import cn.mafangui.hotel.enums.OrderStatus;
import cn.mafangui.hotel.response.AjaxResult;
import cn.mafangui.hotel.response.MsgType;
import cn.mafangui.hotel.response.ResponseTool;
import cn.mafangui.hotel.service.NoticeService;
import cn.mafangui.hotel.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author ：Angular
 * @ProjectName: hotel
 * @Package: cn.mafangui.hotel.controller.worker
 * @ClassName: OpNoticeController
 * @date ：Created in 2021/4/24 10:35
 * @description：公告管理控制层
 * @modified By：
 * @version: v1.0.0$
 */
@RestController
@RequestMapping(value = "/op/notice")
public class OpNoticeController {
    @Autowired
    private NoticeService noticeService;

    /**
     * 添加公告
     * 订单状态默认为未付款状态
     */
    @RequestMapping(value = "/add")
    public AjaxResult addNotice(String title, String content, @DateTimeFormat(pattern = "yyyy-MM-dd") Date time, String userId, String userName, String phone,String role) {
        Notice notice = new Notice(0,title, content, time, Integer.parseInt(userId), userName, phone,role);
        int re = noticeService.insert(notice);
        if (re != 1) return ResponseTool.failed(MsgType.FAILED);
        return ResponseTool.success("添加成功.");
    }


    @RequestMapping(value = "/delete")
    public AjaxResult deleteNotice(int id) {
        int re = noticeService.delete(id);
        if (re != 1) return ResponseTool.failed(MsgType.FAILED);
        return ResponseTool.success("删除成功.");
    }


    @RequestMapping(value = "/update")
    public AjaxResult updateNotice(int id, String title, String content) {
        Notice notice = new Notice(id,title,content);
        int re = noticeService.update(notice);
        if (re != 1) return ResponseTool.failed(MsgType.FAILED);
        return ResponseTool.success("修改成功.");
    }


    @RequestMapping(value = "")
    public AjaxResult getAllNotice() {
        return ResponseTool.success(noticeService.AllNotices());
    }

    @RequestMapping(value = "/count")
    public AjaxResult getNoticeCount() {
        return ResponseTool.success(noticeService.getNoticeCount());
    }

    /**
     * 根据userID查询所有公告
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "/user/{userId}")
    public AjaxResult getByUser(@PathVariable int userId) {
        return ResponseTool.success(noticeService.selectByUserId(userId));
    }


    /**
     * 根据id号查询公告
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}")
    public AjaxResult getById(@PathVariable Integer id) {
        return ResponseTool.success(noticeService.selectById(id));
    }


}
